// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties;

import java.util.Iterator;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.process.CompElement;
import org.dengues.designer.ui.database.parts.DBColumnEditPart;
import org.dengues.designer.ui.database.parts.DBTableEditPart;
import org.dengues.designer.ui.database.parts.ForeignKeyEditPart;
import org.dengues.designer.ui.process.models.CompBlock;
import org.dengues.designer.ui.process.models.CompBlockExit;
import org.dengues.designer.ui.process.models.CompConnection;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.parts.CompConnectionPart;
import org.dengues.designer.ui.process.parts.CompNodePart;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.ForeignKey;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.graphics.Image;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class DenguesEditorPropsLabelProvider extends LabelProvider {

    private final DenguesEditorTypeMapper typeMapper;

    /**
     * Qiang.Zhang.Adolf@gmail.com DenguesEditorPropsLabelProvider constructor comment.
     */
    public DenguesEditorPropsLabelProvider() {
        super();
        typeMapper = new DenguesEditorTypeMapper();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
     */
    @Override
    public Image getImage(Object objects) {
        if (objects == null || objects.equals(StructuredSelection.EMPTY)) {
            return null;
        }
        if (!(objects instanceof IStructuredSelection)) {
            return null;
        }
        Object object = getObject(objects);
        if (object == null) {
            return null;
        } else {
            if (object instanceof DBTableEditPart) {
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_TABLE);
            } else if (object instanceof DBColumnEditPart) {
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_COLUMN);
            } else if (object instanceof ForeignKeyEditPart) {
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_PK);
            } else if (object instanceof CompNodePart) {
                Object model = ((CompNodePart) object).getModel();
                if (model instanceof CompNode) {
                    return ((CompNode) model).getComponent().getIcon16().createImage();
                } else if (model instanceof CompBlock) {
                    return ImageUtil.getImage(IDenguesSharedImage.PALETTE_BLOCK);
                } else if (model instanceof CompBlockExit) {
                    return ImageUtil.getImage(IDenguesSharedImage.PALETTE_BLOCKEXIT16);
                }
            } else if (object instanceof CompConnectionPart) {
                return ImageUtil.getImage(IDenguesSharedImage.VIEW_CONNECTION);
            }

        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
     */
    @Override
    public String getText(Object objects) {
        if (objects == null || objects.equals(StructuredSelection.EMPTY)) {
            return ""; //$NON-NLS-1$
        }
        if (!(objects instanceof IStructuredSelection)) {
            return ""; //$NON-NLS-1$
        }
        Object object = getObject(objects);
        if (object == null) {
            return ""; //$NON-NLS-1$
        } else {
            if (object instanceof DBTableEditPart) {
                return ((DBTable) ((DBTableEditPart) object).getModel()).getName();
            } else if (object instanceof DBColumnEditPart) {
                return ((DBColumn) ((DBColumnEditPart) object).getModel()).getName();
            } else if (object instanceof ForeignKeyEditPart) {
                return ((ForeignKey) ((ForeignKeyEditPart) object).getModel()).getName();
            } else if (object instanceof CompNodePart) {
                Object model = ((CompNodePart) object).getModel();
                if (model instanceof CompNode) {
                    return ((CompNode) model).getUniqueName();
                }
                return ((CompElement) model).getCompName();
            } else if (object instanceof CompConnectionPart) {
                return ((CompConnection) ((CompConnectionPart) object).getModel()).getName();
            }
        }
        return null;
    }

    private Object getObject(Object objects) {
        Assert.isNotNull(objects);
        Object object = objects;
        if (objects instanceof IStructuredSelection) {
            IStructuredSelection selection = (IStructuredSelection) objects;
            object = selection.getFirstElement();
            Class firstClass = typeMapper.mapType(object);
            if (selection.size() == 1) {
                return object;
            }
            if (selection.size() > 1) {
                for (Iterator i = selection.iterator(); i.hasNext();) {
                    Object next = i.next();
                    Class nextClass = typeMapper.mapType(next);
                    if (!nextClass.equals(firstClass)) {
                        object = null;
                        break;
                    }
                }
            }
        }
        return object;
    }
}
